﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using HtmlAgilityPack;

namespace DZDPSpider
{
    public class DataSpider
    {
        /// <summary>
        /// 
        /// </summary>
        public static DataTable GetCuteData()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("店铺名称", Type.GetType("System.String"));
            dt.Columns.Add("店铺评论数", Type.GetType("System.String"));
            dt.Columns.Add("店铺人均消费", Type.GetType("System.String"));
            dt.Columns.Add("店铺类型", Type.GetType("System.String"));
            dt.Columns.Add("店铺地址", Type.GetType("System.String"));
            dt.Columns.Add("店铺经度", Type.GetType("System.String"));
            dt.Columns.Add("店铺纬度", Type.GetType("System.String"));
            dt.Columns.Add("店铺推荐菜", Type.GetType("System.String"));

            // 总页数
            int pages = 50;
            Console.WriteLine("正在进行爬虫中，请稍后");
            for (int i = 1; i <= pages; i++)
            {
                var html = @"http://www.dianping.com/taizhou/ch10/p" + i;

                HtmlWeb web = new HtmlWeb();
                web.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.75 Safari/537.36";
                var htmlDoc = web.Load(html);

                IList<HtmlNode> shopList = htmlDoc.DocumentNode.QuerySelectorAll("#shop-all-list ul li .txt");

                foreach (HtmlNode node in shopList)
                {
                    DataRow dtRow = dt.NewRow();
                    // 名称
                    string shopTitle = node.QuerySelector(".txt .tit a h4").InnerText;
                    dtRow["店铺名称"] = shopTitle;

                    // 评论数
                    HtmlNode shopComment = node.QuerySelector(".txt .comment a.review-num b");
                    string comment = shopComment != null ? shopComment.InnerText + "条点评" : "暂无评分";
                    dtRow["店铺评论数"] = comment;

                    // 平均价格
                    string shopPrice = Regex.Replace(node.QuerySelector(".txt .comment a.mean-price").InnerText, @"\s", "");
                    dtRow["店铺人均消费"] = shopPrice;

                    // 类型
                    string shopType = node.QuerySelectorAll(".tag-addr span.tag")[0].InnerText;
                    dtRow["店铺类型"] = shopType;

                    // 地址
                    string shopAddress = node.QuerySelectorAll(".tag-addr span.tag")[1].InnerText + node.QuerySelector(".tag-addr span.addr").InnerText;
                    dtRow["店铺地址"] = shopAddress;

                    string lngLat = BaiduMapHelper.GetAddress("泰州市" + shopAddress.Replace("#", "号"));

                    // 获得经度
                    string longitude = lngLat.Split(',')[0];
                    dtRow["店铺经度"] = longitude;

                    // 获得纬度
                    string latitude = lngLat.Split(',')[1];
                    dtRow["店铺纬度"] = latitude;

                    IList<HtmlNode> recommendList = node.QuerySelectorAll(".txt .recommend a");
                    // 推荐菜
                    string shopRecommend = "";
                    foreach (HtmlNode recommend in recommendList)
                    {
                        shopRecommend += recommend.InnerText + " ";
                    }
                    dtRow["店铺推荐菜"] = shopRecommend;

                    dt.Rows.Add(dtRow);
                }
            }
            return dt;
        }

        public static DataTable GetHotelData(string typeCode, int pages)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("酒店名称", Type.GetType("System.String"));
            dt.Columns.Add("酒店评论数", Type.GetType("System.String"));
            dt.Columns.Add("酒店标签", Type.GetType("System.String"));
            dt.Columns.Add("酒店起步消费", Type.GetType("System.String"));
            dt.Columns.Add("酒店地址", Type.GetType("System.String"));
            dt.Columns.Add("酒店经度", Type.GetType("System.String"));
            dt.Columns.Add("酒店纬度", Type.GetType("System.String"));

            Console.WriteLine("正在进行爬虫中，请稍后");
            for (int i = 1; i <= pages; i++)
            {
                Console.WriteLine(i);
                string html = HttpHelper.Get(@"http://www.dianping.com/taizhou/hotel/" + typeCode + i);
                HtmlDocument htmlDoc = new HtmlDocument();
                htmlDoc.LoadHtml(html);

                IList<HtmlNode> hotelList = htmlDoc.DocumentNode.QuerySelectorAll("ul.hotelshop-list li.hotel-block");

                foreach (HtmlNode node in hotelList)
                {
                    DataRow dtRow = dt.NewRow();
                    // 酒店名称
                    string hotelTitle = node.QuerySelector(".hotel-info-main h2.hotel-name a.hotel-name-link").InnerText;
                    dtRow["酒店名称"] = hotelTitle;

                    // 酒店评论数
                    string hotelComment = node.QuerySelector(".hotel-remark .remark a").InnerText;
                    dtRow["酒店评论数"] = hotelComment.Substring(1, hotelComment.Length - 2);

                    // 酒店标签
                    IList<HtmlNode> hotelTags = node.QuerySelectorAll(".hotel-info-main .hotel-tags span");
                    string hotelTag = "";
                    foreach (HtmlNode tag in hotelTags)
                    {
                        hotelTag += tag.InnerText + " ";
                    }
                    dtRow["酒店标签"] = hotelTag;

                    // 酒店起步消费
                    string hotelPrice = node.QuerySelector(".hotel-remark .price strong").InnerText;
                    dtRow["酒店起步消费"] = hotelPrice + "元";

                    // 获取当前酒店的id
                    string id = node.Attributes["data-poi"].Value;

                    // 获取酒店地址
                    string hotelDetal = HttpHelper.Get(@"http://www.dianping.com/shop/" + id);
                    HtmlDocument doc = new HtmlDocument();
                    doc.LoadHtml(hotelDetal);
                    string hotelAddress = doc.DocumentNode.QuerySelector("div.hotel-detail-price .hotel-address-box span").InnerText;
                    dtRow["酒店地址"] = hotelAddress;

                    string lngLat = BaiduMapHelper.GetAddress("泰州市" + hotelAddress.Split('：')[1]);

                    // 获得经度
                    string longitude = lngLat.Split(',')[0];
                    dtRow["酒店经度"] = longitude;

                    // 获得纬度
                    string latitude = lngLat.Split(',')[1];
                    dtRow["酒店纬度"] = latitude;

                    dt.Rows.Add(dtRow);
                }
            }
            return dt;
        }
    }
}
